package com.example.administrator.daydayappmarket.software.choice.adapter;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.example.administrator.daydayappmarket.Contacts;
import com.example.administrator.daydayappmarket.R;
import com.example.administrator.daydayappmarket.software.choice.activity.SoftwareChoiceSecondActivity;
import com.example.administrator.daydayappmarket.software.choice.model.SoftwareChoiceTopicApps;
import com.squareup.picasso.Picasso;

import org.greenrobot.greendao.annotation.apihint.Internal;

import java.text.DecimalFormat;
import java.util.List;

import butterknife.ButterKnife;
import butterknife.InjectView;

/**
 * Created by lenovo on 2016/12/26.
 */

public class SoftwareChoiceGridViewAdapter extends BaseAdapter {

    private Context context;
    private List<SoftwareChoiceTopicApps> data;

    public SoftwareChoiceGridViewAdapter(Context context, List<SoftwareChoiceTopicApps> data) {
        this.context = context;
        this.data = data;
    }

    @Override
    public int getCount() {
        return data != null ? data.size() : 0;
    }

    @Override
    public SoftwareChoiceTopicApps getItem(int i) {
        return data.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder=null;
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.item_software_choice_gridview, parent, false);
            viewHolder=new ViewHolder(convertView);
            convertView.setTag(viewHolder);
        }else{
            viewHolder= (ViewHolder) convertView.getTag();
        }
        final SoftwareChoiceTopicApps softwareChoiceTopicApps = data.get(position);
        viewHolder.itemSoftwareChoiceGridViewImageView.setImageResource(R.mipmap.app_default);
        Picasso.with(context).load(Contacts.FRONT_IMG+softwareChoiceTopicApps.getIcon_url()).into(viewHolder.itemSoftwareChoiceGridViewImageView);
        viewHolder.itemSoftwareChoiceGridViewNameTextView.setText(softwareChoiceTopicApps.getName());
        int package_size = softwareChoiceTopicApps.getPackage_size();
        double v =  ((double)package_size / 1024 / 1024);
        DecimalFormat df=new DecimalFormat("#####0.00");
        String format = df.format(v);
        viewHolder.itemSoftwareChoiceGridViewSizeTextView.setText(format+"MB");

        viewHolder.linearLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String id = softwareChoiceTopicApps.getId();
                Intent intent=new Intent(context, SoftwareChoiceSecondActivity.class);
                intent.putExtra("id",id);
                context.startActivity(intent);
            }
        });

        return convertView;
    }

    static class ViewHolder {
        @InjectView(R.id.item_software_choice_gridView_imageView)
        ImageView itemSoftwareChoiceGridViewImageView;
        @InjectView(R.id.item_software_choice_gridView_nameTextView)
        TextView itemSoftwareChoiceGridViewNameTextView;
        @InjectView(R.id.item_software_choice_gridView_SizeTextView)
        TextView itemSoftwareChoiceGridViewSizeTextView;
        @InjectView(R.id.item_software_choice_gridView_button)
        TextView itemSoftwareChoiceGridViewButton; //下方的下载按钮更改为TextView
        @InjectView(R.id.item_software_choice_gridView_linearLayout)
        LinearLayout linearLayout;

        ViewHolder(View view) {
            ButterKnife.inject(this, view);
        }
    }
}
